package com.smart.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpStatus;
import com.smart.auth.dto.MemberDto;
import com.smart.auth.entity.Member;
import com.smart.auth.service.MemberService;
import com.smart.common.vo.ResponseResult;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Member
 */
@Service
public class AuthUserDetailServiceImpl implements UserDetailsService {
    @Resource
    MemberService memberService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //  远程调用 用户服务 获取用户信息
        ResponseResult<Member> result = memberService.getUserInfo(username);
        if (result != null && HttpStatus.HTTP_OK == result.getStatus()) {
            Member member = result.getData();
            MemberDto memberDto = new MemberDto();
            BeanUtil.copyProperties(member, memberDto);
            return memberDto;
        }
        return null;
    }
}
